# 编写一个程序，实现全部替换功能


def replace_all():
    file_name = input("请输入文件名：")
    need_replaced = input("请输入需要替换的单词或者字符：")
    new_word = input("请输入新的单词或字符：")
    content = []
    count = 0
    file = open(file_name)
    for each in file:
        if need_replaced in each:
            count += each.count(need_replaced)
            each = each.replace(need_replaced, new_word)
        content.append(each)
    decide = input("\n文件 %s 中共有%s个[%s]\n您确定要把所有的[%s]替换为[%s]吗？\n[YES/NO]："
                   % (file_name, count, need_replaced, need_replaced, new_word))
    if decide.lower() == "yes":
        file_write = open(file_name, "w")
        file_write.writelines(content)
        file_write.close()
    file.close()


if __name__ == "__main__":
    replace_all()